/*
 * @lc app=leetcode.cn id=24 lang=swift
 *
 * [24] 两两交换链表中的节点
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public var val: Int
 *     public var next: ListNode?
 *     public init() { self.val = 0; self.next = nil; }
 *     public init(_ val: Int) { self.val = val; self.next = nil; }
 *     public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
 * }
 */
class Solution {
    func swapPairs(_ head: ListNode?) -> ListNode? {
        var dummyHead = ListNode(-1)
        dummyHead.next = head
        var cur: ListNode? = dummyHead
        while cur?.next != nil && cur?.next?.next != nil {
            var temp1 = cur?.next
            var temp2 = cur?.next?.next

            temp1?.next = temp2?.next
            temp2?.next = temp1
            cur?.next = temp2
            cur = temp1
        }
        return dummyHead.next
    }
}
// @lc code=end

